function sumSubarrayMins(arr){
    const stack=[]
    let sum=0
    arr.push(0)
    arr.forEach((val,index) => {
        while(arr[stack.at(-1)]>val){
            const center=stack.pop()
            const start=stack.at(-1)??-1
            const end=index
            sum+=(center-start)*(end-center)*arr[center]
        }
        stack.push(index)
    });
    return sum
}
// 测试用例
arr= [3,1,2,4]
console.log(sumSubarrayMins(arr));
